Imports System.Data.SqlClient

Public Class cls_documentoalteracion_

    Implements ICloneable

#Region "Variables ..."
    Private int_iddocumentoalteracion As Integer = 0
    Private int_iddocumento As Integer = 0
    Private int_idalteracion As Integer = 0
    Private str_nombre As String = ""
    Private str_descripcion As String = ""
    Private str_formula As String = ""
    Private int_tipo As cls_alteracion_.enu_tipo = cls_alteracion_.enu_tipo.Valor

    Private col_Items As Collections.Generic.List(Of cls_documentoalteracion_) = Nothing
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return iddocumentoalteracion
        End Get
    End Property
    Property iddocumentoalteracion() As Integer
        Get
            iddocumentoalteracion = int_iddocumentoalteracion
        End Get
        Set(ByVal value As Integer)
            int_iddocumentoalteracion = value
        End Set
    End Property
    Property iddocumento() As Integer
        Get
            iddocumento = int_iddocumento
        End Get
        Set(ByVal value As Integer)
            int_iddocumento = value
        End Set
    End Property
    Property idalteracion() As Integer
        Get
            idalteracion = int_idalteracion
        End Get
        Set(ByVal value As Integer)
            int_idalteracion = value
        End Set
    End Property
    Property nombre() As String
        Get
            nombre = str_nombre
        End Get
        Set(ByVal value As String)
            str_nombre = value
        End Set
    End Property
    Property descripcion() As String
        Get
            descripcion = str_descripcion
        End Get
        Set(ByVal value As String)
            str_descripcion = value
        End Set
    End Property
    Property formula() As String
        Get
            formula = str_formula
        End Get
        Set(ByVal value As String)
            str_formula = value
        End Set
    End Property
    Property tipo() As cls_alteracion_.enu_tipo
        Get
            tipo = int_tipo
        End Get
        Set(ByVal value As cls_alteracion_.enu_tipo)
            int_tipo = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_documentoalteracion_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_documentoalteracion_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_documentoalteracion_)
        While obj_Reader.Read()
            Dim Elemento As New cls_documentoalteracion_
            Elemento.int_iddocumentoalteracion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("iddocumentoalteracion")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("iddocumento"))) = False Then
                Elemento.int_iddocumento = (obj_Reader.GetValue(obj_Reader.GetOrdinal("iddocumento")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idalteracion"))) = False Then
                Elemento.int_idalteracion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idalteracion")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
                Elemento.str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion"))) = False Then
                Elemento.str_descripcion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("formula"))) = False Then
                Elemento.str_formula = (obj_Reader.GetValue(obj_Reader.GetOrdinal("formula")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo"))) = False Then
                Elemento.int_tipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_iddocumentoalteracion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("iddocumentoalteracion")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("iddocumento"))) = False Then
            int_iddocumento = (obj_Reader.GetValue(obj_Reader.GetOrdinal("iddocumento")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idalteracion"))) = False Then
            int_idalteracion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idalteracion")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
            str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion"))) = False Then
            str_descripcion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("formula"))) = False Then
            str_formula = (obj_Reader.GetValue(obj_Reader.GetOrdinal("formula")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo"))) = False Then
            int_tipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo")))
        End If
    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Nuevo = False
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "documentoalteracion__Nuevo"
                .Parameters.Add("@iddocumento", SqlDbType.Int).Value = int_iddocumento
                .Parameters.Add("@idalteracion", SqlDbType.Int).Value = IIf(int_idalteracion = 0, DBNull.Value, int_idalteracion)
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@descripcion", SqlDbType.NVarChar).Value = str_descripcion
                .Parameters.Add("@formula", SqlDbType.NVarChar).Value = str_formula
                .Parameters.Add("@tipo", SqlDbType.Int).Value = int_tipo
                .ExecuteNonQuery()
            End With
            Nuevo = True
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el registro de documentoalteracion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "documentoalteracion__Modificar"
                .Parameters.Add("@iddocumentoalteracion", SqlDbType.Int).Value = int_iddocumentoalteracion
                .Parameters.Add("@iddocumento", SqlDbType.Int).Value = int_iddocumento
                .Parameters.Add("@idalteracion", SqlDbType.Int).Value = IIf(int_idalteracion = 0, DBNull.Value, int_idalteracion)
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@descripcion", SqlDbType.NVarChar).Value = str_descripcion
                .Parameters.Add("@formula", SqlDbType.NVarChar).Value = str_formula
                .Parameters.Add("@tipo", SqlDbType.Int).Value = int_tipo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de documentoalteracion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal iddocumentoalteracion As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "documentoalteracion__Eliminar"
                .Parameters.Add("@iddocumentoalteracion", SqlDbType.Int).Value = iddocumentoalteracion
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el registro de documentoalteracion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal iddocumentoalteracion As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "documentoalteracion__Buscar"
                .Parameters.Add("@iddocumentoalteracion", SqlDbType.Int).Value = iddocumentoalteracion
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de documentoalteracion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "documentoalteracion__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo registro de documentoalteracion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "documentoalteracion__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de documentoalteracion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_documentoalteracion_Clon As New cls_documentoalteracion_
        obj_documentoalteracion_Clon.int_iddocumentoalteracion = Me.int_iddocumentoalteracion
        obj_documentoalteracion_Clon.int_iddocumento = Me.int_iddocumento
        obj_documentoalteracion_Clon.int_idalteracion = Me.int_idalteracion
        obj_documentoalteracion_Clon.str_nombre = Me.str_nombre
        obj_documentoalteracion_Clon.str_descripcion = Me.str_descripcion
        obj_documentoalteracion_Clon.str_formula = Me.str_formula
        obj_documentoalteracion_Clon.int_tipo = Me.int_tipo
        obj_documentoalteracion_Clon.col_Items = Me.col_Items
        Return obj_documentoalteracion_Clon
    End Function

    Function CargarXiddocumento(ByVal int_iddocumento As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXiddocumento = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "documentoalteracion__CargarXiddocumento"
                .Parameters.Add("@iddocumento", SqlDbType.Int).Value = int_iddocumento
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXiddocumento = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarXiddocumento los registros de documentoalteracion_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Resolver(ByRef prm_subtotal As Decimal) As Decimal
        Resolver = 0
        Dim Control As New MSScriptControl.ScriptControl
        Dim tmp_formula As String = formula
        Dim SubTotal As Decimal = prm_subtotal


        tmp_formula = Replace(formula, "[subtotal]", SubTotal)
        tmp_formula = Replace(tmp_formula, ",", ".")

        Control.Language = "VBScript"
        Control.ExecuteStatement("Dim resultado" & vbNewLine & tmp_formula)
        Return Math.Round(Control.Eval("resultado"), int_cantdecimales)

        'DevExpress.XtraEditors.Xtra Throw New Exception("Error de sintaxis, compruebe la formula.", "Formula", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Function

End Class
